﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Data.Sql;


public partial class _Default : System.Web.UI.Page 
{
    //Database_CN KETNOI = new Database_CN();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(DropDownList1.SelectedValue);
        GridView1.DataSource = Xemkhachsan(id);
        GridView1.DataBind();
    }
    public DataTable Xemkhachsan(int Malinhvuc)
    {
        DataTable DTable = new DataTable();
        switch (Malinhvuc)
        {
            case 1:
                {
                    Database_CN.ThietlapKetnoi();
                    SqlDataAdapter Adapter = new SqlDataAdapter("Select* From KS", Database_CN.CN);
                    Adapter.Fill(DTable);
                    Adapter.Update(DTable);
                    return DTable;
                } break;
            case 2:
                {
                    Database_CN.ThietlapKetnoi();
                    SqlDataAdapter Adapter = new SqlDataAdapter("Select* From MSam", Database_CN.CN);
                    Adapter.Fill(DTable);
                    Adapter.Update(DTable);
                    return DTable;
                } break;
            case 3:
                {
                    Database_CN.ThietlapKetnoi();
                    SqlDataAdapter Adapter = new SqlDataAdapter("Select* From GTri", Database_CN.CN);
                    Adapter.Fill(DTable);
                    Adapter.Update(DTable);
                    return DTable;
                } break;
            case 4:
                {
                    Database_CN.ThietlapKetnoi();
                    SqlDataAdapter Adapter = new SqlDataAdapter("Select* From AThuc", Database_CN.CN);
                    Adapter.Fill(DTable);
                    Adapter.Update(DTable);
                    return DTable;
                } break;
            case 5:
                {
                    Database_CN.ThietlapKetnoi();
                    SqlDataAdapter Adapter = new SqlDataAdapter("Select* From LDep", Database_CN.CN);
                    Adapter.Fill(DTable);
                    Adapter.Update(DTable);
                    return DTable;
                } break;
            case 6:
                {
                    Database_CN.ThietlapKetnoi();
                    SqlDataAdapter Adapter = new SqlDataAdapter("Select* From DLich", Database_CN.CN);
                    Adapter.Fill(DTable);
                    Adapter.Update(DTable);
                    return DTable;
                } break;
            case 7:
                {
                    Database_CN.ThietlapKetnoi();
                    SqlDataAdapter Adapter = new SqlDataAdapter("Select* From YTe", Database_CN.CN);
                    Adapter.Fill(DTable);
                    Adapter.Update(DTable);
                    return DTable;
                } break;
            case 8:
                {
                    Database_CN.ThietlapKetnoi();
                    SqlDataAdapter Adapter = new SqlDataAdapter("Select* From TChinh", Database_CN.CN);
                    Adapter.Fill(DTable);
                    Adapter.Update(DTable);
                    return DTable;

                } break;
            default:
                {
                    Database_CN.ThietlapKetnoi();
                    SqlDataAdapter Adapter = new SqlDataAdapter("Select* From GDuc", Database_CN.CN);
                    Adapter.Fill(DTable);
                    Adapter.Update(DTable);
                    return DTable;
                } break;


        }
    } 
}
class Database_CN
{
    private static string StrKetnoi = @"Data Source=JOSEPH-PC\SQLEXPRESS;Initial Catalog=DB_from_phone; Integrated Security=True";
    public static SqlConnection CN;
    public static string ChuoiKetnoi()
    {
        return StrKetnoi;
    }
    public static void ThietlapKetnoi()
    {
        try
        {
            CN = new SqlConnection(StrKetnoi);
            CN.Open();

        }
        catch
        {

        }
    }
    public static void HuyKetnoi()
    {
        CN.Close();
    }
    //Lấy dữ liệu từ bảng
    public DataTable TaoBang(string sql)
    {

        ThietlapKetnoi();
        SqlDataAdapter Adapter = new SqlDataAdapter(sql, CN);
        DataTable DTable = new DataTable();
        Adapter.Fill(DTable);
        Adapter.Update(DTable);
        return DTable;

    }
    //Ham thuc hien lenh executenonquery
    public void ExcuteNonQuery(string sql)
    {
        //Ket noi Database
        ThietlapKetnoi();
        //Thực hiện insert
        SqlCommand cmd = new SqlCommand(sql, CN);
        cmd.ExecuteNonQuery();
        //Hủy kết nối Database
        HuyKetnoi();
        cmd.Dispose();
    }
    
}